<?php
    class Model_donatur extends CI_Model{
        function __construct() {
            parent::__construct();
        }
        function login($username,$password){
            $data = array(
                'username'=>$username,
                'password'=>$password,
                'status'=>'approved');
            $query = $this->db->get_where('donatur',$data);
            if($query->num_rows()==1)
                return TRUE;
            else
                return FALSE;
        }
        function selectbyid($id){
            $array = array('iddonatur'=>$id);
            $query=$this->db->get_where('donatur',$array);
            return $query->result_array();
        }
        function select_status($data)
        {
            if ($data == 'all')
                $query=$this->db->get('donatur');
            else
                {
                    $array = array('status'=>$data);
                    $query=$this->db->get_where('donatur',$array);
                }
                return $query->result_array();
        }

        function select($data){
            $query=$this->db->get_where('donatur',$data);
            return $query->result_array();
        }
		function select_all(){
			$query=$this->db->get('donatur');
            return $query->result_array();
		}
        function select_pagin($kondisi='',$like='',$limit='',$offset=''){
            if($like!='')
            $this->db->like($like);
            $query = $this->db->get_where("donatur",$kondisi,$limit,(($offset-1)*$limit));
            return $query->result_array();
        }

        function isnext($kondisi='',$like='',$limit='',$offset=''){
            if($like!='')
            $this->db->like($like);
            $query = $this->db->get_where("donatur",$kondisi,$limit,($offset*$limit));
            return ($query->num_rows() > 0);
        }
        function isback($offset){
            if($offset==1)
                return false;
            return true;
        }


        function select_cari($nama,$status){
            if ($nama!=null)
                $this->db->like('nama',$nama);
            if($status!=null && $status!='all')
                $this->db->where('status',$status);
            $query=$this->db->get('donatur');
            return $query->result_array();
        }
       
        function daftar($nama, $email, $pekerjaan, $jenis, $alamat, $nohp,$saldo){
            $data = array(
                'nama'=>$nama,
                'email'=>$email,
                'pekerjaan'=>$pekerjaan,
                'jenis'=>$jenis,
                'alamat'=>$alamat,
                'nohp'=>$nohp,
                'saldo'=>$saldo
            );
            $this->db->insert('donatur',$data);
        }
        
        function insert_tambah_donasi($tanggal, $iddonatur, $jumlah_tambahan, $status, $rekening, $bank){
            $data = array(
                'tanggal'=>$tanggal,
                'iddonatur'=>$iddonatur,
                'jumlah_tambahan'=>$jumlah_tambahan,
                'status'=>$status,
                'rekening'=>$rekening,
                'bank'=>$bank
            );
            $this->db->insert('tambah_donasi',$data);
        }
        
        function select_tambah_donasi_by_id_donatur($id){
            //$query=$this->db->get_where('tambah_donasi',$data);
            $query=$this->db->query("SELECT *,t.rekening AS rekening2,t.status AS status2, t.bank AS bank2 FROM tambah_donasi AS t INNER JOIN donatur AS d ON t.iddonatur=d.iddonatur where t.iddonatur = ".$id." ORDER BY t.status desc");
            return $query->result_array();
        }
        
        function select_tambah_donasi_by_id($id)
        {
            $query=$this->db->get_where('tambah_donasi',array('idtambah_donasi'=>$id));
            return $query->result_array();
        }
        
        function select_tambah_donasi()
        {
            $query=$this->db->query("SELECT *,t.rekening AS rekening2,t.status AS status2, t.bank AS bank2 FROM tambah_donasi AS t INNER JOIN donatur AS d ON t.iddonatur=d.iddonatur ORDER BY t.status desc");
            return $query->result_array();
        }
        
        function edit_tambah_donasi($id,$ganti)
        {
            $this->db->where(array('idtambah_donasi'=>$id));
            $this->db->update('tambah_donasi',$ganti);
        }
        
        function tambah_donasi_donatur($id,$tambahan)
        {
            $query=$this->db->query('UPDATE donatur SET saldo = saldo + '.$tambahan.' WHERE iddonatur='.$id);
        }
        
        function update($ganti,$where)
        {
            $this->db->where($where);
            $this->db->update('donatur',$ganti);
        }
    }
?>
